package com.bootdo.hotel.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.hotel.domain.HotelMember;
import com.bootdo.hotel.service.HotelMemberService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 酒店会员
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-22 08:58:25
 */
 
@Controller
@RequestMapping("/hotel/hotelMember")
public class HotelMemberController extends BaseController{


	@Autowired
	private HotelMemberService hotelMemberService;
	
	@ModelAttribute
	public HotelMember get(@RequestParam(required=false) Long id) {
		if (id != null){
			return hotelMemberService.get(id);
		}else{
			return new HotelMember();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/hotel/hotelMember','hotel:hotelMember:hotelMember',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','hotel:hotelMember:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("hotel:hotelMember:hotelMember")	
	String HotelMember(HotelMember hotelMember,HttpServletRequest request,HttpServletResponse response){
	    return "hotel/hotelMember/hotelMember";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("hotel:hotelMember:hotelMember")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<HotelMember> hotelMemberList = hotelMemberService.list(query);
		int total = hotelMemberService.count(query);
		PageUtils pageUtils = new PageUtils(hotelMemberList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("hotel:hotelMember:edit")
	String add(HotelMember hotelMember,HttpServletRequest request,HttpServletResponse response){
	    return "hotel/hotelMember/hotelMemberedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(HotelMember hotelMember) {
		return R.ok().put("i",hotelMember);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hotel:hotelMember:edit")
	public R save( HotelMember hotelMember){
		if(StringUtils.isNotEmpty(hotelMember.getMobile())){
			HotelMember t = 	hotelMemberService.getMemberByMobile(hotelMember.getMobile());
			if(null != t && hotelMember.getId() != null){
				if(!t.getId().equals(hotelMember.getId())){
					return R.error("该电话已经添加过会员");
				}
			}
		}
		if(StringUtils.isEmpty(hotelMember.getLevel())){
			hotelMember.setLevel("WX");
		}
		if(hotelMemberService.save(hotelMember)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hotel:hotelMember:edit")
	public R remove( Long id){
		if(hotelMemberService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hotel:hotelMember:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		hotelMemberService.batchRemove(ids);
		return R.ok();
	}
	
}